//
//  EndRouteVC.swift
//  ShareUmbrella
//
//  Created by niupark on 2017/9/8.
//  Copyright © 2017年 尧尚信息科技. All rights reserved.
//

import UIKit
import YSKit
import SnapKit
import RxSwift
import KMPlaceholderTextView
import TSMessages
import SwiftyJSON

class EndRouteVC: YSBaseViewController {
    var textView: KMPlaceholderTextView!
    var startView: TQStarRatingView!
    let publishButton = UIButton()
    let portraitView = UIImageView()
    let telView = UIButton()
    let subtitleLabel = UILabel()
    let titleLabel = UILabel()
    
    public var helpId: String!
    public var nickName: String!
    public var imageHeader: String!

    var  userInfo:JSON?
    override func buildUI() {
        super.buildUI()
        self.title = I18n("VC.et.title")
        let topBkView = UIImageView().ys.customize { (view) in
            self.view.addSubview(view)
            view.image = UIImage(named: "背景图")
            view.snp.makeConstraints({ (make) in
                make.leading.top.trailing.equalTo(self.view)
            })
        }
        
        let label1 = UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = I18n("VC.et.good title")
            view.textColor = .white
            view.font = YSFont.t02.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(self.view).offset(20)
                make.leading.equalTo(self.view).offset(30)
            })
        }
        let label2 = UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = ""
            view.textColor = .white
            view.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(label1.snp.bottom).offset(10)
                make.leading.equalTo(label1)
            })
        }
        
        let inputBk = UIImageView().ys.customize { (view) in
            self.view.addSubview(view)
            view.image = UIImage(named: "评论框")
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(label2.snp.bottom).offset(10)
                make.leading.equalTo(label2)
                make.trailing.equalTo(self.view).offset(-30)
            })

        }
        
         textView = KMPlaceholderTextView().ys.customize { (view) in
            self.view.addSubview(view)
            view.placeholder = I18n("VC.et.text.placehold")
            view.font = YSFont.t04.font
            view.textColor = .white
            view.backgroundColor = .clear
            view.snp.makeConstraints({ (make) in
                make.top.trailing.leading.bottom.equalTo(inputBk)
                
            })
            
        }
        let label3 = UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = I18n("VC.et.star.title", 15, 5.0)
            view.textColor = .white
            view.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(260)
                make.leading.equalTo(30)
            })
        }
       self.startView =  TQStarRatingView(frame: CGRect(x: 110, y: 253, width: 200, height: 30)).ys.customize { (view) in
            self.view.addSubview(view)
        }
        
        self.publishButton.ys.customize { (view) in
//            view.layer.cornerRadius = 20
//            view.layer.masksToBounds = true
            view.setBackgroundImage(UIImage(named: "发表框"), for: .normal)
            view.setTitle(I18n("common.publish"), for: .normal)
            view.setTitleColor(UIColor(red: 0xFD / 255.0, green: 0xCB / 255.0, blue: 0x6F / 255.0, alpha: 1), for: .normal)
            self.view.addSubview(view)
            
            view.snp.makeConstraints({ (make) in
                make.centerX.equalTo(self.view)
                make.top.equalTo(label3).offset(50)
//                make.width.equalTo(150)
//                make.height.equalTo(40)
            })
            
        }
        
        _ = self.publishButton.rx.tap
            .takeUntil(self.rx.deallocated)
            .subscribe(onNext: { [weak self](RxGestureTypeOptions) in
                
                guard let  `self` = self else {
                    return
                }
                
                _ = YSNetWorkManager.provider.requestJSON(YSStructTarget(AcceptAPI.commentAction(token: Auth.token, star: Float(self.startView.numberOfStar), content: self.textView.text, helpId: self.helpId))).subscribe(onNext: { (data) in
                    
                    self.navigationController?.popToRootViewController(animated: true)
                    
                }, onError: {(ErrorType) in
                    TSMessage.showNotification(withTitle: "提交评论失败", type: .error)
                    
                })

            })

        UILabel().ys.customize { (view) in
            self.view.addSubview(view)
//            view.text = I18n("VC.et.addpoint")
            view.textColor = UIColor(red: 0xFD / 255.0, green: 0xCB / 255.0, blue: 0x6F / 255.0, alpha: 1)
            view.font = YSFont.t04.font
            view.snp.makeConstraints({ (make) in
                make.bottom.equalTo(self.publishButton)
                make.leading.equalTo(self.publishButton.snp.trailing).offset(20)
            })
        }

       let lineview = UIImageView().ys.customize { (view) in
            self.view.addSubview(view)
            view.image = UIImage(named: "结束行程线条")
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(topBkView.snp.bottom).offset(50)
                make.leading.trailing.equalTo(self.view)
            })
        }
        
        UILabel().ys.customize { (view) in
            self.view.addSubview(view)
            view.text = I18n("VC.et.help.person")
            view.textColor = YSColor.c103.color
            view.font = YSFont.t04.font
            view.textAlignment = .right
            view.backgroundColor = self.view.backgroundColor
            view.snp.makeConstraints({ (make) in
                make.centerY.equalTo(lineview)
                make.leading.equalTo(0)
                make.width.equalTo(150)

            })
        }
        
        self.portraitView.ys.customize({ (view) in
            self.view.addSubview(view)
            view.sd_setImage(with: URL(string: imageHeader), placeholderImage: UIImage(named: "空白头像"))
            view.layer.cornerRadius = 80.0/2.0
            view.snp.makeConstraints({ (make) in
                make.top.equalTo(lineview).offset(30)
                make.width.height.equalTo(80);
                make.leading.equalTo(30)
            })
        })
        
        self.titleLabel.ys.customize { (view) in
            self.view.addSubview(view)
            view.text = nickName
            view.textColor = YSColor.c103.color
            view.font = YSFont.t04.font
            if let u = self.userInfo {
                view.text = u["name"].stringValue
            }
            view.snp.makeConstraints({ (make) in
                make.leading.equalTo(self.portraitView.snp.trailing).offset(5)
                make.top.equalTo(self.portraitView).offset(10)
            })

        }
        self.subtitleLabel.ys.customize { (view) in
            self.view.addSubview(view)
            view.text = I18n("VC.et.persion.detail", 5, 1)
            view.textColor = YSColor.c103.color
            view.font = YSFont.t05.font
            view.snp.makeConstraints({ (make) in
                make.leading.equalTo(self.portraitView.snp.trailing).offset(5)
                make.bottom.equalTo(self.portraitView).offset(-10)
            })
        }
        self.telView.ys.customize { (view) in
            self.view.addSubview(view)
            view.setBackgroundImage(UIImage(named: "电话icon"), for: .normal)
            view.snp.makeConstraints({ (make) in
                make.centerY.equalTo(self.portraitView)
                make.trailing.equalTo(self.view).offset(-30)
            })

        }
    }
    
    override func beforePush(_ withdata: Any?) {
        self.userInfo = withdata as? JSON
    }
    
    override func buildRx() {
        super.buildRx()
 
    }
}
